package com.thssh.ui.lsn07.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/20
 * email: zhangyugehu@163.com
 */

public class CanvasHelpView extends View {

    private int mWidth = 500;
    private int mHeight = 500;

    private Paint mPaint;

    public CanvasHelpView(Context context) {
        this(context, null);
    }

    public CanvasHelpView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CanvasHelpView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        drawLine(canvas);
//        drawLines(canvas);
//        drawPath(canvas);
//        drawRegion(canvas);
//        canvasNormal(canvas);
//        canvasTranslate(canvas);
//        canvasRotate(canvas);
//        canvasSkew(canvas);
//        canvasSave(canvas);
//        canvasSaveLayer(canvas);
        canvasClip(canvas);
    }

    private void canvasClip(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
//        canvas.drawColor(Color.RED);
        Path path = new Path();
        path.addOval(new RectF(50, 50, 350, 350), Path.Direction.CCW);
        canvas.clipPath(path);
        canvas.drawRect(0,0,canvas.getWidth(), canvas.getHeight(), mPaint);
    }

    private void canvasSaveLayer(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);
        mPaint.setColor(Color.GREEN);
//        RectF rectF = new RectF(50, 50, 350, 350);
//        canvas.drawRect(rectF, mPaint);
//
//        canvas.saveLayer(rectF, null, Canvas.ALL_SAVE_FLAG);
//        canvas.translate(100, 100);
//        mPaint.setColor(Color.RED);
//        canvas.drawRect(rectF, mPaint);
//
//        canvas.restore();

        canvas.translate(100, 100);
        canvas.drawColor(Color.RED);
        canvas.drawRect(0, 0, 50, 50, mPaint);

        canvas.saveLayer(0,0,canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
        canvas.drawColor(Color.YELLOW);
        canvas.drawRect(50, 50, 100, 100, mPaint);

        canvas.restore();
        canvas.drawRect(0, 0, 50, 50, mPaint);


    }

    private void canvasSave(Canvas canvas) {
        canvas.drawColor(Color.parseColor("#0000ff"));
        canvas.save();

        canvas.clipRect(new RectF(50, 50, 450, 450));
        canvas.drawColor(Color.parseColor("#eeeeee"));
        canvas.save();

        canvas.clipRect(new RectF(100, 100, 400, 400));
        canvas.drawColor(Color.parseColor("#ff0000"));
        canvas.save();

        canvas.clipRect(new RectF(150, 150, 350, 350));
        canvas.drawColor(Color.parseColor("#00ff00"));
        canvas.save();

        canvas.clipRect(new RectF(200, 200, 300, 300));
        canvas.drawColor(Color.parseColor("#00ffff"));
        canvas.save();

        canvas.restore();
        canvas.restore();
        canvas.restore();
        canvas.restore();

        canvas.drawColor(Color.parseColor("#eeff00ff"));
    }

    private void canvasSkew(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);
        RectF rectF = new RectF(5, 5, 300, 400);

        // 斜拉画布
        canvas.skew(1.73f, 0);

        mPaint.setColor(Color.GREEN);
        canvas.drawRect(rectF, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.restore();
    }

    private void canvasRotate(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);
        RectF rectF = new RectF(5, 5, 300, 400);

        // 将画布旋转45度
        canvas.rotate(45);

        mPaint.setColor(Color.GREEN);
        canvas.drawRect(rectF, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(5, 5, canvas.getWidth(), 5, mPaint);
        canvas.drawLine(5, 5, 5, canvas.getHeight(), mPaint);
        canvas.restore();
    }

    private void canvasTranslate(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);
        RectF rectF = new RectF(5, 5, 300, 400);

        // 将画布平移
        canvas.translate(50, 50);

        mPaint.setColor(Color.GREEN);
        canvas.drawRect(rectF, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(5, 5, canvas.getWidth(), 5, mPaint);
        canvas.drawLine(5, 5, 5, canvas.getHeight(), mPaint);
        canvas.restore();
    }

    private void canvasNormal(Canvas canvas) {
        canvas.save();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(15);
        RectF rectF = new RectF(5, 5, 300, 400);

        mPaint.setColor(Color.GREEN);
        canvas.drawRect(rectF, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(5, 5, canvas.getWidth(), 5, mPaint);
        canvas.drawLine(5, 5, 5, canvas.getHeight(), mPaint);
        canvas.restore();
    }

    private void drawRegion(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLUE);
        RectF rectF = new RectF(50, 50, 250, 250);
        Path path = new Path();
        path.addOval(rectF, Path.Direction.CCW);
        // 穿件一块矩形区域
        Region region = new Region(150, 150, 400, 400);
        Region region1 = new Region();
        // 使用path和region区域进行交集
        region1.setPath(path, region);
        region1.op(region, Region.Op.XOR);

        RegionIterator iterator = new RegionIterator(region1);
        Rect tmpRect = new Rect();
        while (iterator.next(tmpRect)){
            canvas.drawRect(tmpRect, mPaint);
        }

    }

    private void drawPath(Canvas canvas) {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(15);
        mPaint.setColor(Color.BLUE);
        RectF rectF = new RectF(100, 100, 300, 400);
        Path path = new Path();
        // 四个角的弧度想x, y值
        float[] radii = {10, 10, 10, 10, 10, 10, 50, 60};
        path.addRoundRect(rectF, radii, Path.Direction.CCW);
//        path.moveTo(10, 10);
//        path.lineTo(200, 200);
//        path.lineTo(100, 180);
//        path.close();
        canvas.drawPath(path, mPaint);
    }

    private void drawLines(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLUE);
        float[] lines = {0, 0, 100, 100, 200, 200, 300, 300};
        canvas.drawLines(lines, mPaint);
        // 画虚线
//        canvas.drawLines(lines, 20, 2, mPaint);
    }

    private void drawLine(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(50,50, 200, 200, mPaint);
    }
}
